import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:myapp/api/Http.dart';
import 'package:myapp/api/Urls.dart';
import 'package:myapp/model/RecommentGoodsModel.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'PageThreeItem.dart';

/*
  第三页
 */

class PageThree extends StatefulWidget {
  @override
  _PageThreeState createState() => _PageThreeState();
}

class _PageThreeState extends State<PageThree> with AutomaticKeepAliveClientMixin{
  RefreshController _refreshController =
      RefreshController();

  void _onRefresh() async {
    // monitor network fetch
    page = 0;
    lastId = "";
    getPageThreeData();
    // if failed,use refreshFailed()
    _refreshController.refreshCompleted();
  }

  void _onLoading() async {
    // monitor network fetch
    // if failed,use loadFailed(),if no data return,use LoadNodata()
    page++;
    getPageThreeData();
    _refreshController.loadComplete();
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getPageThreeData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: MaterialClassicHeader(),
        footer: CustomFooter(
          builder: (BuildContext context, LoadStatus mode) {
            Widget body;
            if (mode == LoadStatus.idle) {
              body = Text("上拉加载");
            } else if (mode == LoadStatus.loading) {
              body = CupertinoActivityIndicator();
            } else if (mode == LoadStatus.failed) {
              body = Text("加载失败！点击重试！");
            } else if (mode == LoadStatus.canLoading) {
              body = Text("松手,加载更多!");
            } else {
              body = Text("没有更多数据了!");
            }
            return Container(
              height: 55.0,
              child: Center(child: body),
            );
          },
        ),
        controller: _refreshController,
        onRefresh: _onRefresh,
        onLoading: _onLoading,
        child: ListView.builder(
          itemBuilder: (c, i) =>
              Card(child: Center(child: PageThreeItem(listBean[i]))),
          itemExtent: 100.0,
          itemCount: listBean.length,
        ),
      ),
    );
  }


  String lastId = "";
  int page = 0;

  List<ListBean> listBean = List();
  void getPageThreeData() {
    Map<String, dynamic> paraMap = Map();
    paraMap["offset"] = page*20;
    if (lastId.isNotEmpty) {
      paraMap["list_id"] = lastId;
    }
    paraMap["channel_type"] = 0;
    paraMap["limit"] = 20;
    DioUtils.postHttp(Urls.RecommendGoodsUrl, parameters: paraMap,
        onSuccess: (data) {
      RecommentGoodsModel recommentGoodsModel = RecommentGoodsModel.fromMap(jsonDecode(data));
      lastId =recommentGoodsModel.goodsBasicDetailResponse.listId;
      setState(() {
        listBean.addAll(recommentGoodsModel.goodsBasicDetailResponse.list);
      });
    }, onError: (err) {
      print(err);
    });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
